<?php
/**
 * Function:
 * Description:
 * Abo 2018/10/11 20:22
 * Email: abo2013@foxmail.com
 */

$start = microtime( true );
$workers = [];
// 尽快打开所有连接
$urls = [
    'http://www.baidu.com',
    'http://www.sina.com.cn',
    'http://www.qq.com',
    'http://www.baidu.com',
    'http://www.baidu.com?search=abo_o1',
    'http://www.baidu.com?search=imooc',
    'http://www.baidu.com?search=哈哈哈',
];

foreach( $urls as $k2url => $v2url ) {
    // 子进程
    $process = new swoole_process( function( swoole_process $pro ) use( $k2url, $v2url )  {
        $content = curlData( $v2url );
        // echo $content . PHP_EOL; // >> 内容输入到 管道内
        $pro->write( $content . PHP_EOL ); // 两种方法一样
    }, true );

    $pid = $process->start();
    $workers[ $pid ] = $process;
}

// >> 取出 之前输入到管道的内容
foreach( $workers as $v2process ){
    echo $v2process->read();
}

$end = microtime( true );

echo 'use time:' . ( $end - $start );
/**
 * 模拟请求url数据
 * @param $url
 */
function curlData( $url )
{
    sleep( 1 );
    return $url . ' success' . PHP_EOL;
}